// Time:  O(nlogn)
// Space: O(n)

#include <string>
#include <vector>
using namespace std;

class Solution {
public:
    int findMinDifference(vector<string>& timePoints) {
        if(timePoints.empty() || timePoints.size() > 1440) {
            return 0;
        }

        static const int MINUTES_PER_DAY = 60 * 24;
        vector<int> minutes;
        minutes.reserve(timePoints.size());

        for(const string& time: timePoints) {
            if(time.length() != 5 || time[2] != ':') {
                throw invalid_argument("Invalid time format");
            }
            int hrs = stoi(time.substr(0, 2));
            int mins = stoi(time.substr(3, 2));
            if(hrs > 23 || mins > 59) {
                throw invalid_argument("Invalid time format");
            }
            minutes.push_back(hrs * 60 + mins);
        }

        sort(minutes.begin(), minutes.end());

        int minDiff = MINUTES_PER_DAY;
        for(size_t i = 1; i < minutes.size(); ++i) {
            minDiff = min(minDiff, minutes[i] - minutes[i - 1]);
        }

        minDiff = min(minDiff, MINUTES_PER_DAY - (minutes.back() - minutes.front()));
        return minDiff;
    }
};
